package com.wzx.data.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzx.basics.baseVo.PageVo;
import com.wzx.basics.baseVo.Result;
import com.wzx.basics.utils.PageInfo;
import com.wzx.basics.utils.PageUtil;
import com.wzx.basics.utils.ResultUtil;
import com.wzx.data.entity.Question;
import com.wzx.data.utils.NullUtils;
import com.wzx.data.entity.Answer;
import com.wzx.data.service.IAnswerService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wzx.data.vo.AnswerVo;
import com.wzx.data.vo.QuestionVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 答复表 前端控制器
 * @author wzx
 * @since 2023/03/20
 */
@Slf4j
@RestController
@Api(tags = "答复表管理接口")
@RequestMapping("/wzx/answer")
@Transactional
public class AnswerController {

    @Autowired
    private IAnswerService iAnswerService;

    @RequestMapping(value = "/getOne", method = RequestMethod.GET)
    @ApiOperation(value = "查询单条答复表")
    public Result<Answer> get(@RequestParam String id){
        return new ResultUtil<Answer>().setData(iAnswerService.getById(id));
    }

    @RequestMapping(value = "/count", method = RequestMethod.GET)
    @ApiOperation(value = "查询全部答复表个数")
    public Result<Long> getCount(){
        return new ResultUtil<Long>().setData(iAnswerService.count());
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ApiOperation(value = "查询全部答复表")
    public Result<List<Answer>> getAll(){
        return new ResultUtil<List<Answer>>().setData(iAnswerService.list());
    }



    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "增改答复表")
    public Result<Answer> saveOrUpdate(Answer answer){
        if(iAnswerService.saveOrUpdate(answer)){
            return new ResultUtil<Answer>().setData(answer);
        }
        return ResultUtil.error();
    }

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ApiOperation(value = "新增答复表")
    public Result<Answer> insert(Answer answer){
        iAnswerService.saveOrUpdate(answer);
        return new ResultUtil<Answer>().setData(answer);
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation(value = "编辑答复表")
    public Result<Answer> update(Answer answer){
        iAnswerService.saveOrUpdate(answer);
        return new ResultUtil<Answer>().setData(answer);
    }

    @RequestMapping(value = "/delByIds", method = RequestMethod.POST)
    @ApiOperation(value = "删除答复表")
    public Result<Object> delByIds(@RequestParam String[] ids){
        for(String id : ids){
            iAnswerService.removeById(id);
        }
        return ResultUtil.success();
    }
    @RequestMapping(value = "/getByPage", method = RequestMethod.GET)
    @ApiOperation(value = "查询回答")
    public Result getByPage(@ModelAttribute AnswerVo vo , @ModelAttribute PageVo pageVo){
        System.out.println(vo);
        final LambdaQueryWrapper<Answer> lambda = new QueryWrapper<Answer>().lambda();
        this.buildCondition(lambda,vo);
        final IPage<Answer> page = iAnswerService.page(new Page<>(pageVo.getPageNumber(), pageVo.getPageSize()), lambda);
        PageInfo<Answer> pageInfo = new PageInfo<>(pageVo.getPageNumber(), pageVo.getPageSize());
        pageInfo.setTotal((int) page.getTotal());
        pageInfo.setTotalList(page.getRecords());
        return ResultUtil.data(pageInfo);
    }
    /**
     * 构建查询条件
     * @param lambda
     * @param vo
     */
    private void buildCondition(LambdaQueryWrapper<Answer> lambda, AnswerVo vo) {
        if(vo==null) {
            return;
        }
        if (ObjectUtils.isNotEmpty(vo.getQuestionId())){
            lambda.eq(Answer::getQuestionId, vo.getQuestionId());
        }
        if (ObjectUtils.isNotEmpty(vo.getAnswerId())){
            lambda.eq(Answer::getAnswerId, vo.getAnswerId());
        }

        //根据startTime
        if (ObjectUtils.isNotEmpty(vo.getStartTime())){
            lambda.ge(Answer::getCreateTime, vo.getStartTime());
        }
        //根据endTime
        if (ObjectUtils.isNotEmpty(vo.getEndTime())){
            lambda.le(Answer::getCreateTime, vo.getEndTime());
        }
        lambda.eq(Answer::getDelFlag,0);
        lambda.orderByDesc(Answer::getCreateTime);
    }
}
